package es.denisneuf.core.imanagers 
{

	/**
	* @author Daniel Alvaro
	* @work dalvaro@ifoline.com
	* @gmail znpobbx@gmail.com
	* @msn denisneuf@hotmail.com
	* @twitter znpobbx
	**/

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import es.denisneuf.loading.BulkLoader;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	
	import flash.system.System;
	
	import es.denisneuf.core.LoggerSupport;
	import com.luminicbox.log.Logger;
	public class Template extends Sprite implements iTemplate
	{
		public static var className:String = "Template";
		public static var packageName:String = "es.denisneuf.core.imanagers";
		public static var version:String = "1.0.10";
		public static var author:String = "Daniel Alvaro";
		
		private var _container:MovieClip;
		private var _swf:String;
		private var _mp3:String;
		private var bulkLoader : BulkLoader;
		private var _path:String = "modulo01/es/media/";
		private var soundContainer:Object = new Object();
		private var channel:SoundChannel;
		
		private var logmanager:LoggerSupport = new LoggerSupport();
		private var miLog:Logger = logmanager.getLogger();
		
			
		public function Template():void 
		{	
			//miLog = logmanager.getLogger(); 
		}
	

		public function drawTemplate (data:XML):void
		{
		}

	
		public function destroyTemplate():void
		{
			
			//TODO Efficient clean up mode
			//trace ('Trying to destroy >>' + soundContainer['sound']);
			
			miLog.log("BEFORE System.totalMemory: " + System.totalMemory);
			
			channel.stop();
			_container.removeChildAt(0);
			soundContainer['sound'] = null;
			delete soundContainer['sound'];
			soundContainer = null;
			bulkLoader = null;
			
			//trace (System);
			
			System.gc();
				
			miLog.log("AFTER System.totalMemory: " + System.totalMemory);

			
		}
		
		public function renderDataFromXML (_xml:XML):void
		{
			_swf = _xml['MEDIA'].@swf;
			_mp3 = _xml['MEDIA'].@mp3;
			
			loadContent();
			
		}
		
		
	   	private function _onProgressHandler(evt : ProgressEvent) : void
	   	{    
	       //var percent:uint = ( evt.bytesLoaded  * 100 ) / evt.bytesTotal;
	       //trace("Loaded" , evt.bytesLoaded," of ",  evt.bytesTotal + " % " + percent + " scaleX " + percent / 100 );
	    }	
	    
	    
	    private function soundCompleteHandler(event:Event):void 
	    {
            //trace("completeHandler: " + event);
        }
	    
	    
	   	private function _onCompleteHandler(evt : ProgressEvent) : void
	   	{
   			var flash:MovieClip = bulkLoader.getMovieClip(_path + _swf, true);
           	var sound:Sound = bulkLoader.getSound(_path + _mp3, true);
           	soundContainer['sound'] = sound;
            channel = soundContainer['sound'].play();
            channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
            _container.addChild(flash);
	   	}        	
		
		private function loadContent():void
		{
			bulkLoader = new BulkLoader('Loader#'+Math.random());
			bulkLoader.add(_path + _swf);
		    bulkLoader.add(_path + _mp3);
    		bulkLoader.addEventListener(BulkLoader.COMPLETE, _onCompleteHandler);
   			// event fired when loading progress has been made:
    		bulkLoader.addEventListener(BulkLoader.PROGRESS, _onProgressHandler);		    
			// start loading all items
    		bulkLoader.start();	
		}
		
		public function set container(newcontainer:MovieClip):void
		{
			_container = newcontainer;
		}

		public function get container():MovieClip
		{
			return _container;
		}		

		public function destroy():void
		{
		}

	}
}
